﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TripsLogic.controller;
using TripsLogic.WeSStatus;
using System.Data;

namespace TripsLogic
{
    public partial class log_in : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                loadLang();
            }
            
        }

        private void loadLang()
        {
            litEmail.Text = LangController.getLng("litEmail.Text", "Email");
            litLogIn.Text = lbtLogin.Text = this.Title =  LangController.getLng("litLogIn.Text", "Log in");
            litPassword.Text = LangController.getLng("litPassword.Text", "Password");

            string sForgetPass = LangController.getLng("litForgetPass.Text", "Forget password");
            hplForgetPass.Text = sForgetPass + "?";
            hplForgetPass.NavigateUrl = sForgetPass.ToLower().Replace(' ', '-');

            litNewUser.Text = LangMsgController.getLngMsg("litNewUser.Text","New user? Click <a class='link3' href='register'>here</a> for register.");

            litFacebookLogin.Text = LangController.getLng("litFacebookLogin.Text", "Using Facebook to log in");
            litFacebookLoginContent.Text = LangMsgController.getLngMsg("litFacebookLogin.Text", "Quisque nulla. Vestibulum libero nisl, porta vel, sceleris eget, malesuada at, neque. Vivamus eget nibh leo vel metus. Nulla facilisi. Aenean nec eros.");

            this.reqValEmail.ErrorMessage = this.reqValPassword.ErrorMessage = LangController.getLng("litRequired.Text", "(*)");
            this.rgeValEmail.ErrorMessage = LangController.getLng("litInvalidFormat.Text", "Invalid format");
        }

        protected void lbtLogin_Click(object sender, EventArgs e)
        {
            WeSSysUser.LoginResult result = new WeSSysUser.WeSSysUserClient().logIn(txtEmail.Text, txtPassword.Text);
            if (result.sysUserResultStatus.statusCode == LoginResult.STATUS_INVALID_CREDENTIAL_CODE)
            {
                this.lblLoginResponse.ForeColor = System.Drawing.Color.Red;
                this.lblLoginResponse.Text = "<br/>" + result.sysUserResultStatus.statusText;
                this.Title = LangController.getLng("litWebsiteTitle.Text", "TripsLogic") + " - " + result.sysUserResultStatus.statusText;
            }
            else if (result.sysUserResultStatus.statusCode == LoginResult.STATUS_INACTIVATED_USER_CODE)
            {
                this.lblLoginResponse.ForeColor = System.Drawing.Color.Red;
                this.lblLoginResponse.Text = "<br/>" + result.sysUserResultStatus.statusText + "<br/>" + LangMsgController.getLngMsg("litInActivatedAccount.Text", "<font color='#000'>Please check your email to activate your account or you can <a class='link3' href='reset-activated-code'>reset activated code</a>.</font>");
                this.Title = LangController.getLng("litWebsiteTitle.Text", "TripsLogic") + " - " + result.sysUserResultStatus.statusText;
            }
            else if (result.sysUserResultStatus.statusCode == LoginResult.STATUS_DISABLE_USER_ERROR_CODE)
            {
                this.lblLoginResponse.ForeColor = System.Drawing.Color.Red;
                this.lblLoginResponse.Text = "<br/>" + result.sysUserResultStatus.statusText + "<br/>";
                this.Title = LangController.getLng("litWebsiteTitle.Text", "TripsLogic") + " - " + result.sysUserResultStatus.statusText;
            }
            else if (result.sysUserResultStatus.statusCode == LoginResult.STATUS_GENERAL_ERROR_CODE)
            {
                this.lblLoginResponse.ForeColor = System.Drawing.Color.Red;
                this.lblLoginResponse.Text = "<br/>" + result.sysUserResultStatus.statusText + "<br/>" + LangMsgController.getLngMsg("litTryAgain.Text", "<font color='#000'>Please try again later.</font>");
                this.Title = LangController.getLng("litWebsiteTitle.Text", "TripsLogic") + " - " + result.sysUserResultStatus.statusText;
            }
            else
            {
                Session["Email"] = txtEmail.Text;
                DataTable dt = SysUserController.getUserByEmail(txtEmail.Text);
                if (dt != null && dt.Rows.Count > 0)
                    Session["UserId"] = dt.Rows[0]["Id"].ToString();
                if (Session["RefUrl"] != null && Session["RefUrl"].ToString() != "")
                {
                    if (Session["RefUrl"].ToString().Contains("info-"))
                        Response.Redirect(Session["RefUrl"].ToString() + "#_comment");
                    else
                        Response.Redirect(Session["RefUrl"].ToString());
                }
                else
                    Response.Redirect("home", true);
            }
        }
    }
}